package com.example.xinlijiankangsys.mapper;

import com.example.xinlijiankangsys.pojo.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {
    @Insert("INSERT INTO comment_tb(post_id, user_id, content, like_count, create_time) " +
            "VALUES(#{postId}, 0, #{content}, 0, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "commentId")
    int insert(Comment comment);

    @Select("SELECT * FROM comment_tb WHERE post_id = #{postId} ORDER BY create_time DESC")
    @Results({
            @Result(property = "commentId", column = "comment_id", id = true),
            @Result(property = "postId", column = "post_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "content", column = "content"),
            @Result(property = "likeCount", column = "like_count"),
            @Result(property = "createTime", column = "create_time")
    })
    List<Comment> selectByPostId(Long postId);

    @Update("UPDATE comment_tb SET like_count = like_count + 1 WHERE comment_id = #{commentId}")
    int incrementLikeCount(Long commentId);

    @Delete("DELETE FROM comment_tb WHERE comment_id = #{commentId}")
    int deleteById(Long commentId);
}